home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-tools-backends-2.0 / scripts / Users / Groups.pm next >
Encoding:
Perl POD Document  |  2009-04-09  |  7.2 KB  |  311 lines

  1. #-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. #
  3. # Copyright (C) 2000-2001 Ximian, Inc.
  4. #
  5. # Authors: Hans Petter Jansson <hpj@ximian.com>,
  6. #          Arturo Espinosa <arturo@ximian.com>,
  7. #          Tambet Ingo <tambet@ximian.com>.
  8. #          Grzegorz Golawski <grzegol@pld-linux.org> (PLD Support)
  9. #
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU Library General Public License as published
  13. # by the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. # GNU Library General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Library General Public License
  22. # along with this program; if not, write to the Free Software
  23. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  24.  
  25. package Users::Groups;
  26.  
  27. # enum like for verbose group array positions
  28. my $i = 0;
  29. my $LOGIN  = $i++;
  30. my $PASSWD = $i++;
  31. my $GID    = $i++;
  32. my $USERS  = $i++;
  33.  
  34. # quite generic data
  35. $group_names = "/etc/group";
  36.  
  37. # Where are the tools?
  38. $cmd_groupdel = &Utils::File::locate_tool ("groupdel");
  39. $cmd_groupadd = &Utils::File::locate_tool ("groupadd");
  40. $cmd_groupmod = &Utils::File::locate_tool ("groupmod");
  41.  
  42. $cmd_delgroup = &Utils::File::locate_tool ("delgroup");
  43. $cmd_addgroup = &Utils::File::locate_tool ("addgroup");
  44.  
  45. $cmd_usermod  = &Utils::File::locate_tool ("usermod");
  46. $cmd_gpasswd  = &Utils::File::locate_tool ("gpasswd");    
  47. $cmd_pw       = &Utils::File::locate_tool ("pw");
  48.  
  49. sub del_group
  50. {
  51.   my ($group) = @_;
  52.  
  53.   if ($Utils::Backend::tool{"system"} eq "FreeBSD")
  54.   {
  55.     $command = "$cmd_pw groupdel -n \'" . $$group[$LOGIN] . "\'";
  56.   }
  57.   else
  58.   {
  59.     $command  = ($cmd_delgroup) ? $cmd_delgroup : $cmd_groupdel;
  60.     $command .= " \'" . $$group[$LOGIN] . "\'";
  61.   }
  62.  
  63.   &Utils::File::run ($command);
  64. }
  65.  
  66. # This is only for Linux and SunOS,
  67. # pw groupadd manages this in FreeBSD
  68. sub add_user_to_group
  69. {
  70.   my ($group, $user) = @_;
  71.   my ($command);
  72.  
  73.   if ($Utils::Backend::tool{"system"} eq "SunOS")
  74.   {
  75.     my ($groups, @arr);
  76.  
  77.     $groups = &Utils::File::run_backtick ("groups $user");
  78.     $groups =~ s/.*://;
  79.     chomp ($groups);
  80.  
  81.     @arr = split (/ /, $groups);
  82.     push @arr, $group;
  83.     $groups = join (',', @arr);
  84.     $groups =~ s/^,//;
  85.     $groups =~ s/,$//;
  86.  
  87.     $command = "$cmd_usermod -G $groups $user";
  88.   }
  89.   else
  90.   {
  91.     $command = "$cmd_gpasswd -a \'" . $user . "\' " . $group;
  92.   }
  93.  
  94.   &Utils::File::run ($command);
  95. }
  96.  
  97. # This is only for Linux and SunOS,
  98. # pw groupdel manages this in FreeBSD
  99. sub delete_user_from_group
  100. {
  101.   my ($group, $user) = @_;
  102.   my ($command);
  103.  
  104.   if ($Utils::Backend::tool{"system"} eq "SunOS")
  105.   {
  106.     my ($groups, @arr);
  107.  
  108.     $groups = &Utils::File::run_backtick ("groups $user");
  109.     $groups =~ s/.*://;
  110.     chomp ($groups);
  111.  
  112.     # delete the user
  113.     $groups =~ s/[ \t]+$group//;
  114.  
  115.     @arr = split (/ /, $groups);
  116.     $groups = join (',', @arr);
  117.     $groups =~ s/^,//;
  118.     $groups =~ s/,$//;
  119.     
  120.     $command = "$cmd_usermod -G $groups $user";
  121.   }
  122.   else
  123.   {
  124.     $command = "$cmd_gpasswd -d \'" . $user . "\' \'" . $group . "\'";
  125.   }
  126.  
  127.   &Utils::File::run ($command);
  128. }
  129.  
  130. sub add_group
  131. {
  132.   my ($group) = @_;
  133.   my ($u, $user, $users);
  134.  
  135.   $u = $$group[$USERS];
  136.  
  137.   if ($Utils::Backend::tool{"system"} eq "FreeBSD")
  138.   {
  139.     $users = join (",", sort @$u);
  140.       
  141.     $command = "$cmd_pw groupadd -n \'" . $$group[$LOGIN] .
  142.       "\' -g \'" . $$group[$GID] .
  143.       "\' -M \'" . $users . "\'";
  144.  
  145.     &Utils::File::run ($command);
  146.   }
  147.   else
  148.   {
  149.     if ($cmd_addgroup)
  150.     {
  151.       $command = "$cmd_addgroup " .
  152.           "--gid \'" . $$group[$GID] . "\' " . $$group[$LOGIN];
  153.     }
  154.     else
  155.     {
  156.       $command = "$cmd_groupadd -g \'" . $$group[$GID] .
  157.           "\' " . $$group[$LOGIN];
  158.     }
  159.  
  160.     &Utils::File::run ($command);
  161.  
  162.     foreach $user (sort @$u)
  163.     {
  164.       &add_user_to_group ($$group[$LOGIN], $user);
  165.     }
  166.   }
  167. }
  168.  
  169. sub change_group
  170. {
  171.     my ($old_group, $new_group) = @_;
  172.   my (%users, %user, $users_arr, $str);
  173.  
  174.     my ($n, $o, $users, $i, $j, $max_n, $max_o, $r, @tmp); # for iterations
  175.  
  176.   if ($Utils::Backend::tool{"system"} eq "FreeBSD")
  177.   {
  178.     $users_arr = $$new_group[$USERS];
  179.     $str = join (",", sort @$users_arr);
  180.  
  181.     $command = "$cmd_pw groupmod -n \'" . $$old_group[$LOGIN] .
  182.         "\' -g \'" . $$new_group[$GID] .
  183.         "\' -l \'" . $$new_group[$LOGIN] .
  184.         "\' -M \'" . $str . "\'";
  185.  
  186.     &Utils::File::run ($command);
  187.   }
  188.   else
  189.   {
  190.     $command = "$cmd_groupmod -g \'" . $$new_group[$GID] .
  191.         "\' -n \'" . $$new_group[$LOGIN] . "\' " .
  192.         "\'" . $$old_group[$LOGIN] . "\'";
  193.   
  194.     &Utils::File::run ($command);
  195.  
  196.     # Let's see if the users that compose the group have changed.
  197.     if (!Utils::Util::struct_eq ($$new_group[$USERS], $$old_group[$USERS]))
  198.     {
  199.       $users{$_} |= 1 foreach (@{$$new_group[$USERS]});
  200.       $users{$_} |= 2 foreach (@{$$old_group[$USERS]});
  201.  
  202.       foreach $user (keys %users)
  203.       {
  204.         $state = $users{$user};
  205.  
  206.         if ($state == 2)
  207.         {
  208.           # users with state 2 are those that only appeared
  209.           # in the old group configuration, so we must delete them
  210.           &delete_user_from_group ($$new_group [$LOGIN], $user);
  211.         }
  212.         elsif ($state == 1)
  213.         {
  214.           # users with state 1 are those who were added
  215.           # to the new group configuration
  216.           &add_user_to_group ($$new_group[$LOGIN], $user);
  217.         }
  218.       }
  219.     }
  220.   }
  221. }
  222.  
  223. sub get
  224. {
  225.   my ($ifh, @groups, $group_last_modified);
  226.   my (@line, $copy, @a);
  227.  
  228.   $ifh = &Utils::File::open_read_from_names($group_names);
  229.   return unless ($ifh);
  230.  
  231.   # Parse the file.
  232.   @groups = ();
  233.  
  234.   while (<$ifh>)
  235.   {
  236.     chomp;
  237.  
  238.     # FreeBSD allows comments in the group file. */
  239.     next if &Utils::Util::ignore_line ($_);
  240.  
  241.     @line = split ':', $_, -1;
  242.     @a = split ',', pop @line;
  243.     push @line, [@a];
  244.     $copy = [@line];
  245.     push (@groups, $copy);
  246.   }
  247.  
  248.   &Utils::File::close_file ($ifh);
  249.  
  250.   return \@groups;
  251. }
  252.  
  253. sub get_files
  254. {
  255.   my @arr;
  256.  
  257.   push @arr, $group_names;
  258.   return \@arr;
  259. }
  260.  
  261. sub set
  262. {
  263.   my ($config) = @_;
  264.   my ($old_config, %groups);
  265.   my (%config_hash, %old_config_hash);
  266.  
  267.   if ($config)
  268.   {
  269.     # Make backup manually, otherwise they don't get backed up.
  270.     &Utils::File::do_backup ($group_names);
  271.  
  272.     $old_config = &get ();
  273.  
  274.     foreach $i (@$config)
  275.     {
  276.       $groups{$$i[$LOGIN]} |= 1;
  277.       $config_hash{$$i[$LOGIN]} = $i;
  278.     }
  279.  
  280.     foreach $i (@$old_config)
  281.     {
  282.         $groups{$$i[$LOGIN]} |= 2;
  283.       $old_config_hash{$$i[$LOGIN]} = $i;
  284.     }
  285.  
  286.     # Delete all groups that only appeared in the old configuration
  287.     foreach $i (sort (keys (%groups)))
  288.     {
  289.       $state = $groups{$i};
  290.  
  291.       if ($state == 1)
  292.       {
  293.         # Groups with state 1 have been added to the config
  294.         &add_group ($config_hash{$i});
  295.       }
  296.       elsif ($state == 2)
  297.       {
  298.         # Groups with state 2 have been deleted from the config
  299.         &del_group ($old_config_hash{$i});
  300.       }
  301.       elsif (($state == 3) &&
  302.              (!Utils::Util::struct_eq ($config_hash{$i}, $old_config_hash{$i})))
  303.       {
  304.         &change_group ($old_config_hash{$i}, $config_hash{$i});
  305.       }
  306.     }
  307.   }
  308. }
  309.  
  310. 1;
  311.